package com.zafu.dao;

import com.zafu.entity.AddressInfo;
import com.zafu.entity.StoreInfo;
import com.zafu.util.ElmDbConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
 * ClassName: AddressBean
 * Package: com.zafu.dao
 * Description:
 *
 * @Author Wang
 * @Create 2024/12/20 21:01
 * @Version 1.0
 */
public class AddressBean {
    private Connection conn;

    public AddressBean() {
    }
    /**
     * 添加一条地址记录
     * @param info，首页店家对应的信息
     * @return
     */
    public int Add(AddressInfo info) {
        int count = -1;
        conn =  ElmDbConn.GetConnection();

        if (conn == null) {
            System.out.println("数据库连接失败");
            return -1;
        }

        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append("insert into Address")
                    .append("(address, username, user_phone, chosen,id )")
                    .append("values ( ?, ?, ?, ?,?) ");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString() );
            int i = 0;
            st.setString(++i, info.getAddress());
            st.setString(++i, info.getUsername());
            st.setString(++i, info.getUser_phone());
            st.setInt(++i,info.getChosen());
            st.setInt(++i,info.getId());

            count = st.executeUpdate();
            System.out.printf("插入%d条记录\n", count);
            return count;
        }
        catch (SQLException e) {
            System.out.println("插入失败："+ e.getMessage());
            e.printStackTrace();
            return -1;
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch (SQLException e) {
                    System.out.println("关闭连接失败\n" + e.getMessage());
                }
            }
        }
    }


    /**
     * 根据id删除一条记录
     * @param id
     * @return
     */
    public int DelById(int id) {
        int count = -1; // 受影响的记录条数
        conn = ElmDbConn.GetConnection();// 获得连接

        try {
            StringBuffer sBuffer = new StringBuffer();
            // sql语句
            sBuffer.append( "delete from Address " )
                    .append( " where id = ? " );
            PreparedStatement st = conn.prepareStatement(  sBuffer.toString() );
            int i = 1;
            // id, 主键
            st.setInt(i, id);
            ++i;
            // 执行sql
            count = st.executeUpdate( );// 执行语句
            System.out.printf( "删除%d条记录\n", count );
            return count;
        }
        catch(SQLException e ){
            System.out.printf( "删除失败:" + e.getMessage() );
            e.printStackTrace();
            return -1;
        }
        finally {
            if( conn != null ){
                try{
                    conn.close();
                }
                catch( SQLException e ){
                    System.out.printf( "关闭连接失败\n" + e.getMessage()  );
                }// try
            }// if
        }
    }
    /**
     * 获取所有地址
     * @return
     */
    public ArrayList<AddressInfo> GetAll() {
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        ArrayList<AddressInfo> list = new ArrayList<>();
        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append( " select address, username, user_phone, chosen, id")
                    .append(" from Address");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString());
            ResultSet rs = st.executeQuery();
            while(rs.next()) {
                AddressInfo info = GetDataFromResultSet(rs);
                list.add(info);
            }
        }
        catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch ( SQLException e ) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return list;
    }

    /**
     * 寻找data
     * @param searchData
     * @return
     */
    public ArrayList<AddressInfo> SearchByAddress(String searchData) {
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        ArrayList<AddressInfo> list = new ArrayList<>();
        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append( " select address, username, user_phone, chosen, id ")
                    .append(" from Address")
                    .append(" where address LIKE ?");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString());
            st.setString(1, "%" + searchData + "%");
            ResultSet rs = st.executeQuery();
            while(rs.next()) {
                AddressInfo info = GetDataFromResultSet(rs);
                list.add(info);
            }
        }
        catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch ( SQLException e ) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return list;
    }


    private AddressInfo GetDataFromResultSet(ResultSet rs ){

        AddressInfo info = null;
        try{
            String address = rs.getString( "address" );
            String username = rs.getString( "username" );
            String user_phone = rs.getString( "user_phone" );
            int chosen = rs.getInt( "chosen" );
            int id = rs.getInt(("id"));
            info = new AddressInfo(address, username, user_phone, chosen, id);

        }catch( SQLException e ){
            System.out.printf( "数据库查询失败\n" + e.getMessage()  );
            e.printStackTrace();
        }
        return info;
    }
}
